BudgeterDataContext.cs
Language: C#
Last Modified: 2020-06-27 1:58:31 PM UTC
File Size: 5940 bytes
Last Modified: 2020-06-27 1:58:31 PM UTC
File Size: 5940 bytes
http://www.penguinstew.ca/example/dataviewer/Model/Connection/BudgeterDataContext.cs
using System.Data.Linq;
using System.Linq;
using System;
using System.Reflection;
using System.Data.Linq.Mapping;
namespace Budgeter.Model.Connection
{
/// <summary>
/// Data context class for the budgeter project
/// </summary>
public class BudgeterDataContext : DataContext
{
#region Variables
/// <summary>
/// Table containing transaction information
/// </summary>
public Table<Transaction> Transactions = null;
/// <summary>
/// Table containing transaction category information
/// </summary>
public Table<Category> Categories = null;
/// <summary>
/// Table containing global budget type information
/// </summary>
public Table<BudgetType> BudgetTypes = null;
/// <summary>
/// Table mapping budget types to categories
/// </summary>
public Table<BudgetCategory> BudgetCategories = null;
/// <summary>
/// Table containing monthly instances of budget types
/// </summary>
public Table<BudgetMonth> BudgetMonths = null;
/// <summary>
/// Table containing month information
/// </summary>
public Table<Month> Months = null;
/// <summary>
/// Table containing global special account information
/// </summary>
public Table<SpecialAccount> SpecialAccounts = null;
/// <summary>
/// Table containing monthly instances of special accounts
/// </summary>
public Table<SpecialAccountMonth> SpecialAccountMonths = null;
/// <summary>
/// Table containing global recurring Payment information
/// </summary>
public Table<RecurringPayment> RecurringPayments = null;
/// <summary>
/// Table containing information about specific items belong to recurring Payments
/// </summary>
public Table<RecurringPaymentItem> RecurringPaymentItems = null;
/// <summary>
/// Table containing monthly instances of recurring Payments
/// </summary>
public Table<RecurringPaymentMonth> RecurringPaymentMonths = null;
/// <summary>
/// Private instance variable
/// </summary>
private static BudgeterDataContext g_instance;
#endregion
#region Constructor
/// <summary>
/// Creates a new data context using the default connection string from the manager
/// </summary>
public BudgeterDataContext()
:this(new ConnectionStringManager().GetConnectionString())
{
}
/// <summary>
/// Creates a new data context based on the given connection string
/// </summary>
/// <param name="connectionString">The connection string to use</param>
private BudgeterDataContext(string connectionString)
: base(connectionString)
{
}
#endregion
#region Properties
public static BudgeterDataContext Instance
{
get
{
if (g_instance == null)
{
g_instance = new BudgeterDataContext();
}
return g_instance;
}
}
#endregion
#region Public Methods
/// <summary>
/// Creates a new data context with the given connection manager Reinitialize
/// </summary>
/// <param name="connectionManager">The connection manager used to get the new connection string</param>
public static void Reinitialize(ConnectionStringManager connectionManager)
{
g_instance = new BudgeterDataContext(connectionManager.GetConnectionString());
}
/// <summary>
/// Deletes the database this data context refers to and then recreates it.
/// </summary>
public void ReCreateDatabase()
{
this.DeleteDatabase();
this.CreateDatabase();
}
/// <summary>
/// Tests the database connection to make sure it has all the correct tables
/// </summary>
public void TestServer()
{
//Create database if it doesn't already exist
if (!this.DatabaseExists())
{
this.CreateDatabase();
}
//Test tables
var specialAccounts = this.SpecialAccounts.FirstOrDefault();
var SpecialAccountMonths = this.SpecialAccountMonths.FirstOrDefault();
var BudgetCategories = this.BudgetCategories.FirstOrDefault();
var BudgetMonths = this.BudgetMonths.FirstOrDefault();
var BudgetTypes = this.BudgetTypes.FirstOrDefault();
var Categories = this.Categories.FirstOrDefault();
var Months = this.Months.FirstOrDefault();
var Transactions = this.Transactions.FirstOrDefault();
var RecurringPayments = this.RecurringPayments.FirstOrDefault();
var RecurringPaymentMonths = this.RecurringPaymentMonths.FirstOrDefault();
var RecurringPaymentItems = this.RecurringPaymentItems.FirstOrDefault();
}
#endregion
/// <summary>
/// Refreshes each item to overwrite current values
/// </summary>
/// <param name="entities">The entities to refresh</param>
public void Refresh(params IRefreshableTable[] entities)
{
foreach(IRefreshableTable entity in entities.Where(o => o != null))
{
this.Refresh(RefreshMode.OverwriteCurrentValues, entity);
entity.RefreshEntitySets(this);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180